package com.laxcen.controller2;


import com.github.pagehelper.PageInfo;
import com.laxcen.smoke.config.MyLog;
import com.laxcen.smoke.dto.req.*;
import com.laxcen.smoke.dto.res.UserMessageRes;
import com.laxcen.smoke.dto.res.UserResourceRes;
import com.laxcen.smoke.service.UsersService;
import com.laxcen.smoke.util.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.List;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UsersService userService;

    @ApiOperation(value = "添加用户",notes =
            "请求信息： \n" +
                    "&emsp; username &nbsp; : &nbsp; 用户名 \n" +
                    "&emsp; account &nbsp; : &nbsp; 账号 \n" +
                    "&emsp; password &nbsp; : &nbsp; 密码  \n" +
                    "&emsp; roleflag &nbsp; : &nbsp; 权限标志 0超级管理员 1普通管理员 \n" +
                    "&emsp; onoroff &nbsp; : &nbsp; 启用禁用标志 0启用 2禁用 \n" +
                    "&emsp; province &nbsp; : &nbsp; 省(超级管理员不传此字段) \n" +
                    "&emsp; city &nbsp; : &nbsp; 市 (超级管理员不传此字段)\n" +
                    "&emsp; area &nbsp; : &nbsp; 区 (超级管理员不传此字段)\n" +
                    "返回码： \n" +
                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
    @PostMapping(value = "/insert")
    @MyLog(operation = "添加用户")
    @RequiresPermissions("user:add")
    public Response insert(@RequestBody UserInsertReq userInsertReq) throws Exception {
        return userService.insert(userInsertReq.getUsername(),userInsertReq.getAccount(),userInsertReq.getPassword(),userInsertReq.getRoleflag(),userInsertReq.getOnoroff(),userInsertReq.getProvince(),userInsertReq.getCity(),userInsertReq.getArea());
    }

    @ApiOperation(value = "修改用户",notes =
            "请求信息： \n" +
                    "&emsp; username &nbsp; : &nbsp; 用户名 \n" +
                    "&emsp; account &nbsp; : &nbsp; 账号 \n" +
                    "&emsp; password &nbsp; : &nbsp; 密码  \n" +
                    "&emsp; roleflag &nbsp; : &nbsp; 权限标志 0超级管理员 1运维 2维修 \n" +
                    "&emsp; onoroff &nbsp; : &nbsp; 启用禁用标志 0启用 2禁用 \n" +
                    "&emsp; province &nbsp; : &nbsp; 省(超级管理员不传此字段) \n" +
                    "&emsp; city &nbsp; : &nbsp; 市 (超级管理员不传此字段)\n" +
                    "&emsp; area &nbsp; : &nbsp; 区 (超级管理员不传此字段)\n" +
                    "返回码： \n" +
                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
    @PostMapping(value = "/update")
    @MyLog(operation = "修改用户")
    @RequiresPermissions("user:edit")
    public Response update(@RequestBody UserUpdateReq userUpdateReq) throws Exception {
        return userService.update(userUpdateReq.getId(),userUpdateReq.getUsername(),userUpdateReq.getAccount(),userUpdateReq.getPassword(),userUpdateReq.getRoleflag(),userUpdateReq.getOnoroff(),userUpdateReq.getProvince(),userUpdateReq.getCity(),userUpdateReq.getArea());
    }

    @ApiOperation(value = "删除用户",notes =
            "请求信息： \n" +
                    "&emsp; id &nbsp; : &nbsp; 用户id \n" +
                    "返回码： \n" +
                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
    @PostMapping(value = "/delete")
    @MyLog(operation = "删除用户")
    @RequiresPermissions("user:delete")
    public Response delete(@RequestBody CommonOneIdReq commonOneIdReq){
        return userService.delete(commonOneIdReq.getId());
    }

    @ApiOperation(value = "批量删除用户",notes =
            "请求信息： \n" +
                    "&emsp; id &nbsp; : &nbsp; 用户id(多个用英文,隔开) \n" +
                    "返回码： \n" +
                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
    @PostMapping(value = "/deletemore")
    @MyLog(operation = "批量删除用户")
    @RequiresPermissions("user:batchDelete")
    public Response deletemore(@RequestBody CommonOneIdReq commonOneIdReq){
        return userService.deletemore(commonOneIdReq.getId());
    }

    @ApiOperation(value = "获取用户列表",notes =
            "请求信息： \n" +
                    "&emsp; pageNo &nbsp; : &nbsp; 当前页数 \n" +
                    "&emsp; pageSize &nbsp; : &nbsp; 每页的数据条数 \n" +
            "返回信息： \n" +
                    "&emsp; id &nbsp; : &nbsp; 用户id \n" +
                    "&emsp; username &nbsp; : &nbsp; 用户名 \n" +
                    "&emsp; account &nbsp; : &nbsp; 账号 \n" +
                    "&emsp; roleflag &nbsp; : &nbsp; 权限标志 0超级管理员 1运维 2维修 \n" +
                    "&emsp; onoroff &nbsp; : &nbsp; 启用禁用标志 0启用 2禁用 \n" +
                    "&emsp; province &nbsp; : &nbsp; 省(超级管理员不传此字段) \n" +
                    "&emsp; city &nbsp; : &nbsp; 市(超级管理员不传此字段) \n" +
                    "&emsp; area &nbsp; : &nbsp; 区(超级管理员不传此字段) \n" +
                    "&emsp; createtime &nbsp; : &nbsp; 创建时间 \n" +
                    "&emsp; lastlogintime &nbsp; : &nbsp; 最近登录时间 \n" +
                    "&emsp; logincount &nbsp; : &nbsp; 登录次数 \n" +
                    "返回码： \n" +
                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
    @PostMapping(value = "/getalllist")
    @RequiresPermissions("users")
    public Response<PageInfo<UserMessageRes>> getAllList(@RequestBody PageInfoReq pageInfoReq){
        if (pageInfoReq.getPageNo()==null){
            pageInfoReq.setPageNo(1);
        }
        if (pageInfoReq.getPageSize()==null){
            pageInfoReq.setPageSize(10);
        }
        if (pageInfoReq.getPageNo()==null&&pageInfoReq.getPageSize()==null){
            pageInfoReq.setPageNo(1);
            pageInfoReq.setPageSize(10);
        }
        return userService.getAllUser(pageInfoReq.getPageNo(),pageInfoReq.getPageSize());
    }

    @ApiOperation(value = "导航栏指定条件搜索用户信息",notes =
            "请求信息： \n" +
                    "&emsp; account &nbsp; : &nbsp; 账号 \n" +
                    "&emsp; username &nbsp; : &nbsp; 用户名/昵称 \n" +
                    "&emsp; pageNo &nbsp; : &nbsp; 当前页数 \n" +
                    "&emsp; pageSize &nbsp; : &nbsp; 每页的数据条数 \n" +
            "返回信息： \n" +
                    "&emsp; id &nbsp; : &nbsp; 用户id \n" +
                    "&emsp; username &nbsp; : &nbsp; 用户名 \n" +
                    "&emsp; account &nbsp; : &nbsp; 账号 \n" +
                    "&emsp; roleflag &nbsp; : &nbsp; 权限标志 0超级管理员 1运维 2维修 \n" +
                    "&emsp; onoroff &nbsp; : &nbsp; 启用禁用标志 0启用 2禁用 \n" +
                    "&emsp; province &nbsp; : &nbsp; 省(超级管理员不传此字段) \n" +
                    "&emsp; city &nbsp; : &nbsp; 市 (超级管理员不传此字段)\n" +
                    "&emsp; area &nbsp; : &nbsp; 区 (超级管理员不传此字段)\n" +
                    "&emsp; createtime &nbsp; : &nbsp; 创建时间 \n" +
                    "&emsp; lastlogintime &nbsp; : &nbsp; 最近登录时间 \n" +
                    "&emsp; logincount &nbsp; : &nbsp; 登录次数 \n" +

                    "返回码： \n" +
                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
    @PostMapping(value = "/getdefine")
    @RequiresPermissions("users")
    public Response getDefineList(@RequestBody UserSelectTabReq userSelectTabReq) throws ParseException {
        if (userSelectTabReq.getPageNo()==null){
            userSelectTabReq.setPageNo(1);
        }
        if (userSelectTabReq.getPageSize()==null){
            userSelectTabReq.setPageSize(10);
        }
        if (userSelectTabReq.getPageNo()==null&&userSelectTabReq.getPageSize()==null){
            userSelectTabReq.setPageNo(1);
            userSelectTabReq.setPageSize(10);
        }
        return userService.getDefineList(userSelectTabReq.getAccount(),userSelectTabReq.getUsername(),userSelectTabReq.getPageNo(),userSelectTabReq.getPageSize());
    }

    @ApiOperation(value = "登录界面",notes =
            "请求信息： \n" +
                    "&emsp; account &nbsp; : &nbsp; 账号 \n" +
                    "&emsp; password &nbsp; : &nbsp; 密码 \n" +
                    "返回信息： \n" +
                    "&emsp; id &nbsp; : &nbsp; 用户id \n" +
                    "&emsp; username &nbsp; : &nbsp; 用户名 \n" +
                    "&emsp; account &nbsp; : &nbsp; 账号 \n" +
                    "&emsp; roleflag &nbsp; : &nbsp; 权限标志 0超级管理员 1运维 2维修 \n" +
                    "&emsp; onoroff &nbsp; : &nbsp; 启用禁用标志 0启用 1禁用 \n" +
                    "&emsp; createtime &nbsp; : &nbsp; 创建时间 \n" +
                    "&emsp; lastlogintime &nbsp; : &nbsp; 最近登录时间 \n" +
                    "&emsp; logincount &nbsp; : &nbsp; 登录次数 \n" +
                    "返回码： \n" +
                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
    @PostMapping(value = "/sign")
    public Response Sign(@RequestBody UserSignReq userSignReq, HttpServletRequest request, HttpServletResponse response)  {
            return userService.sign(userSignReq.getAccount(),userSignReq.getPassword(),request,response);
    }

    @ApiOperation(value = "分配角色",notes =
            "请求信息： \n" +
                    "&emsp; userid &nbsp; : &nbsp; 用户id \n" +
                    "&emsp; roleid &nbsp; : &nbsp; 角色id \n" +
                    "&emsp; username &nbsp; : &nbsp; 当前登录的用户名 \n" +
                    "返回信息： \n" +
                    "&emsp; id &nbsp; : &nbsp; 用户id \n" +
                    "&emsp; username &nbsp; : &nbsp; 用户名 \n" +
                    "&emsp; account &nbsp; : &nbsp; 账号 \n" +
                    "&emsp; roleflag &nbsp; : &nbsp; 权限标志 0超级管理员 1运维 2维修 \n" +
                    "&emsp; onoroff &nbsp; : &nbsp; 启用禁用标志 0启用 1禁用 \n" +
                    "&emsp; createtime &nbsp; : &nbsp; 创建时间 \n" +
                    "返回码： \n" +
                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
    @PostMapping(value = "/distribute")
    @MyLog(operation = "分配角色")
    @RequiresPermissions("user:allotRole")
    public Response distribute(@RequestBody DistributeUserReq distributeUserReq)  {
        return userService.distribute(distributeUserReq.getUserid(),distributeUserReq.getRoleid());
    }

    @ApiOperation(value = "获取用户关联的所有资源",notes =
            "请求信息： \n" +
                    "&emsp; id &nbsp; : &nbsp; 用户id \n" +
                    "返回信息： \n" +
                    "&emsp; id &nbsp; : &nbsp; 用户id \n" +
                    "&emsp; name &nbsp; : &nbsp; 资源名称 \n" +
                    "&emsp; parent_id &nbsp; : &nbsp; 父级id \n" +
                    "&emsp; url &nbsp; : &nbsp; 资源路径 \n" +
                    "&emsp; permission &nbsp; : &nbsp; 资源权限 \n" +
                    "&emsp; sort &nbsp; : &nbsp; 排序 \n" +
                    "返回码： \n" +
                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
    @PostMapping(value = "/userresource")
    public Response<List<UserResourceRes>> getUserResource(@RequestBody CommonOneIdReq commonOneIdReq)  {
        return userService.getUserResource(commonOneIdReq.getId());
    }

}
